﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.statistics
{
    public partial class reportapp : System.Web.UI.Page
    {
        protected string[] order_Report = new string[] { "", "", "" };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[DTKeys.SESSION_APP_INFO] != null)
            {
                string appNum = Session[DTKeys.SESSION_APP_INFO].ToString(); 
                order_Report = GetOrderReportByAppNum(appNum);
            }
            else
            {
                order_Report = GetOrderReportByAppNum("");
            }
        }

        private string[] GetOrderReportByAppNum(string appNum)
        {
            BLL.statistics bll = new BLL.statistics();

            System.Data.DataSet dsOrder = bll.GetOrderReportByAppNum();
            if (appNum!="")
            {
                dsOrder = bll.GetOrderReportByAppNum(appNum);
            }

            string [] arr_series = new string[2];
            string xAxis = string.Empty;
            string yAxis = string.Empty;
            string[] strCities = null;
            
            //1：提取城市地址
            foreach (System.Data.DataRow r in dsOrder.Tables[0].Rows) 
            {
                string strCity = r["city"].ToString();
                if (xAxis.IndexOf(strCity) == -1) 
                {
                    xAxis += "'"+strCity + "',";
                }
            }
            if (xAxis.Length > 0)
            {
                xAxis = xAxis.Remove(xAxis.Length - 1, 1);
                strCities = xAxis.Replace("'", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);


                //2：提取各地区的销量
                string temp_yAxis = string.Empty;
                foreach (string city in strCities)
                {
                    System.Data.DataRow[] rows = dsOrder.Tables[0].Select("city = '" + city + "'");
                    if (rows.Length > 0)
                    {
                        temp_yAxis += rows[0]["ordercount"].ToString() + ",";
                    }
                    else
                    {
                        temp_yAxis += "0,";
                    }
                }

                if (temp_yAxis.Length > 0)
                {
                    temp_yAxis = temp_yAxis.Remove(temp_yAxis.Length - 1, 1);
                }

                yAxis += "{ type: 'column', name: '订单总数', data: [" + temp_yAxis + "] }, ";


                //3：提取各地区的销售额
                string temp_totalMoney = string.Empty;
                foreach (string city in strCities)
                {
                    System.Data.DataRow[] rows = dsOrder.Tables[0].Select("city = '" + city + "'");
                    decimal totalMoney = 0;
                    foreach (System.Data.DataRow r in rows)
                    {
                        totalMoney += decimal.Parse(r["totalMoney"].ToString());
                    }

                    temp_totalMoney += totalMoney.ToString() + ",";
                }
                temp_totalMoney = temp_totalMoney.Remove(temp_totalMoney.Length - 1, 1);
                yAxis += "{type: 'spline', name: '总金额', yAxis: 1, data: [" + temp_totalMoney + "], marker: {lineWidth: 2,lineColor: Highcharts.getOptions().colors[3],fillColor: 'white'}}";

            }
            else 
            {
                yAxis += "{ type: 'column', name: '订单总数', data: [0] }, ";
                yAxis += "{type: 'spline', name: '总金额', yAxis: 1, data: [0], marker: {lineWidth: 2,lineColor: Highcharts.getOptions().colors[3],fillColor: 'white'}}";
            }

            arr_series[0] = xAxis;
            arr_series[1] = yAxis;
            return arr_series;
        }


    }
}